﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xModel;
using Yule8xCommon.Typelib;
using Yule8xModel.PageModels;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Withdraw
{
    public partial class Default : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser;
        protected DataTable dtBankcard;
        protected WithDrawPageModels.WithDrawPageData pageData;
        protected int smscountdown,user_id;
        protected string mobile_mask, bankcard;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("withdraw"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                Response.Redirect(WechatUrls.GetUserInfoUrl("withdraw"), true);
            }
            */

            pageData = WithDrawPageModels.GetPageData(wxUser.user_id);
            smscountdown = AppConf.SMSCodeTimedown;
            if (null == pageData)
            {
                Response.Write("账户异常!");
                Response.End();
            }
            string action = RequestUtil.FormString("action"),sqlstmt;
            if ("submit" == action)
            {
                DoSubmit();
            }
            else
            {
                this.CheckBankCardInfo(wxUser.user_id);

                // 获取支付方式
                sqlstmt = string.Format("select top 1 bank_holder+':'+bank_account as title from t_bankcard where user_id={0}", wxUser.user_id);
                bankcard= DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue(sqlstmt,"").ToString();
            }
            mobile_mask = UserModels.GetMobile(wxUser.user_id, true);
        }
        private void CheckBankCardInfo(int user_id)
        {
            // 检测提现资料是否完善
            int count = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteCount(string.Format("select count(0) from t_bankcard where user_id={0}", user_id));
            if (count < 1)
            {
                Response.Redirect("WithDrawResult.aspx?result=invalid_profile", true);
            }
        }
        private void DoSubmit()
        {
            string mobile = UserModels.GetMobile(wxUser.user_id);
            decimal amount = RequestUtil.FormDecimal("amount");
            string yzmcode = RequestUtil.FormString("yzm");
            if (!UserCaptchaModels.CheckCaptchaCode(wxUser.user_id, "withdrawconfirm", yzmcode))
            {
                RedirectUtil.RedirectTimer(false, "出错啦!", new string[] { "短信验证码已失效!" });
            }
            if (amount < 1 || string.IsNullOrEmpty(yzmcode))
            {
                RedirectUtil.RedirectTimer(false, "出错啦!", new string[] { "表单数据有误!" });
            }
            // 获取支付方式
            string sqlstmt = string.Format("select top 1 id from t_bankcard where user_id={0}", wxUser.user_id);
            int bankcard_id = int.Parse(DbFactory.Init(AppInfo.conn_yule8x_agent).GetValue(sqlstmt, "0").ToString());
            string msg;
            bool result=WithDrawModels.WithDrawSubmit(wxUser.user_id,mobile,amount,bankcard_id,out msg);
            if (result)
            {
                Response.Redirect("WithDrawResult.aspx?result=success");
            }
            else
            {
                Response.Redirect("WithDrawResult.aspx?result=fail");
            }

        }
    }
}